import { app } from 'electron'
import path from 'path';
import initWindow from './windows/index';

import DB from './DB/init';

let mainWindow;
if (process.env.NODE_ENV !== 'development') {
  global.__static = path.join(__dirname, '/static').replace(/\\/g, '\\\\')
};

const gotTheLock = app.requestSingleInstanceLock()

if (!gotTheLock) {
  app.quit()
} else {
  app.on('second-instance', (event, commandLine, workingDirectory) => {
    // 当运行第二个实例时,将会聚焦到myWindow这个窗口
    if (mainWindow) {
      mainWindow.show();
      if (mainWindow.isMinimized()) mainWindow.restore()
      mainWindow.focus();
    } else {
      mainWindow = initWindow(app);
    }
  })
  // 创建 myWindow, 加载应用的其余部分, etc...
  app.on('ready', () => {
    DB(async function (DB) {
      mainWindow = initWindow(app);
    });
  });
  
  app.on('activate', () => {
    if (mainWindow === null) {
      mainWindow = initWindow(app);
    }
  });

};
